<!--
 * @Author: zhangpeng
 * @Date: 2024-11-14 10:25:35
 * @FilePath: \scientific_research_manager_web\src\views\project\components\Details\ProjectDocUpload.vue
 * @LastEditTime: 2024-12-02 15:17:12
 * @Description: 文档上传
-->
<!-- ***立项前上传的文档，不可删除。 -->
<template>
  <div>
    <div class="flex justify-end mb-16px">
      <!-- <el-button type="primary" icon="Upload">上传文件</el-button> -->
      <FileUpload :show-list="false" :is-show-tip="false" :model-value="fileInfo" @update:modelValue="update" />
    </div>
    <el-table border :data="tableData" style="width: 100%">
      <el-table-column type="index" width="48" label="序号" />
      <el-table-column prop="AttachmentFileInfoDTO.name" label="文件名" />
      <el-table-column prop="uploadTime" label="上传时间" />
      <el-table-column width="195" label="操作">
        <template #default="scope">
          <!-- 创建项目上传文档 -->
          <el-button v-if="!props?.projectId" type="danger" link
            @click="delFile(scope?.row, scope.$index)">删除</el-button>
          <!-- 立项前上传的文档不删除 -->
          <template v-else-if="scope?.row?.isDelete">
            <el-button link type="primary">查看</el-button>
            <el-button link type="primary">下载</el-button>
          </template>
          <!-- 可删除 -->
          <template v-else>
            <el-button link type="primary">查看</el-button>
            <el-button link type="primary">下载</el-button>
            <el-button type="danger" link @click="delFile(scope?.row, scope.$index)">删除</el-button>
          </template>


        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script lang="ts" setup>
import { projectDocumentCreateApi, projectDocumentListDetailApi, projectDocumentDeleteApi } from '@/api/project'
import type { ProjectDocumentCreateDTO, ProjectDocumentDetailVO, OssFileInfoDTO } from '@/api/project/types'

const props = defineProps<{
  projectId?: string
}>()

const tableData = ref<ProjectDocumentDetailVO[]>([
])

const fileList = computed(() => {
  return tableData.value?.map((item) => {
    return item
  })
})

const fileInfo = ref([])

// 获取项目文档list
const { run: projectDocumentList } = useRequest(projectDocumentListDetailApi, {
  manual: true,
  onSuccess: (res) => {
    console.log(1)
    tableData.value = res
  }
})


// 创建文档
const { run: projectDocumentCreate } = useRequest(projectDocumentCreateApi, {
  manual: true,
  onSuccess: (res) => {
    if (res?.isSuccess) {
      console.log(2)
      projectDocumentList({ projectId: props?.projectId })
    }
  }
})

// 删除文档
const { run: projectDocumentDelete } = useRequest(projectDocumentDeleteApi, {
  manual: true,
  onSuccess: (res) => {
    if (res?.isSuccess) {
      projectDocumentList({ projectId: props?.projectId })
    }
  }
})

function update(val) {
  fileInfo.value = []
  const params = {
    fileId: val[0]?.id,
    projectId: props?.projectId,
  } as ProjectDocumentCreateDTO

  if (props?.projectId) {
    projectDocumentCreate(params)
  } else {
    tableData.value?.push(val[0]!)
  }
}

function delFile(file: ProjectDocumentDetailVO, index: number) {
  if (props?.projectId) {
    projectDocumentDelete({ id: file?.id })
  } else {
    tableData.value.splice(index, 1)
  }
}

onMounted(() => {
  if (props?.projectId) {
    projectDocumentList({ projectId: props?.projectId })
  }
})

defineExpose({ fileList })
</script>